package net.com.sunkey.messagehandler;

import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.Map.Entry;

import net.com.sunkey.protobuf.PluginNotice.PluginNoticeData;
import net.com.sunkey.protobuf.PropertyData.Property;
import net.com.sunkey.protobuf.Protocol;
import net.com.sunkey.serverlogic.PluginData;
import net.com.sunkey.serverlogic.Server;

/**
 * @author kohachiro
 *
 */
public class PluginNoticeHandler {

	/**
	 * 
	 */
	public static int invoke(Object data, Object channel)  throws Exception {
		System.out.println("["+channel.hashCode()+"]["+Protocol.MessageHandler.PluginNotice_VALUE+"]PluginNotice");

		
		PluginNoticeData.Builder builder=PluginNoticeData.newBuilder();
		PluginData pluginData=(PluginData)data;
		builder.setName(pluginData.getName());
		builder.setAction(pluginData.getAction());
		
		System.out.println("PluginNotice:"+pluginData.getName()+"."+pluginData.getAction());
		
		Property.Builder propertyBuilder;
		Entry<String, String> entry;		
		for (Iterator<Entry<String, String>> er = pluginData.getProperties().entrySet().iterator();er.hasNext();){
			entry=er.next();
			propertyBuilder=Property.newBuilder();
			propertyBuilder.setName(entry.getKey());
			propertyBuilder.setValue(entry.getValue());
			builder.addProperties(propertyBuilder);
		}		
		
		PluginNoticeData message=builder.build();
		Server.sendResponse(Protocol.MessageHandler.PluginNotice_VALUE, ByteBuffer.wrap(message.toByteArray()), channel);
		return Protocol.MessageHandler.PluginNotice_VALUE;
	}


}
